<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    protected function successJson($data = null, $jsonp = false)
    {
        $res = [
            'code'=>0,
            'errcode'=>'0',
            'msg'=>'success',
        ];
        if($data) {
            $res['data'] = $data;
        }
        if ($jsonp) {
            return response()->jsonp(request()->input('callback'), $res);
        }
        return response()->json($res);
    }

    protected function failJson($errmsg, $param = [], $jsonp = false)
    {
        $errcode = intval(trans('errmsg.api.'.$errmsg.'.0'));
        $msg = trans('errmsg.api.'.$errmsg.'.1', $param);
        $err = [
            'code'=>1,
            'errcode'=>$errcode,
            'message'=>"[{$errcode}]{$msg}"
        ];
        if ($jsonp) {
            return response()->jsonp(request()->input('callback'), $err);
        }
        return response()->json($err);
    }
}
